/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo;

import view.book.GeneralInf;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JDialog;
import entity.RoomType;
import entity.Room;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import constants.*;

/**
 *
 * @author Administrator
 */
public class RoomI extends javax.swing.JDialog {

    /**
     * Creates new form RoomInf
     */
    public RoomI(JDialog parent, boolean modal, ArrayList<Room> list) {
        super(parent, modal);
        
        this.parent =(GeneralInf)parent;
        dcbmRoomType = new DefaultComboBoxModel();
        dlmSelectedRoom = new DefaultListModel();
        dlmRoom = new DefaultListModel();
        roomService = new dao.Room();
        roomTypeService = new dao.RoomType();
        listSelectedRoom = new ArrayList<Room>();
        listSelectedRoom = list;
        initComponents();
        // Hien thi tuong doi so voi Frame parent
        int x;
        int y;
        Point topLeft = parent.getLocationOnScreen();
        Dimension parentSize = parent.getSize();

        Dimension mySize = getSize();

        if (parentSize.width > mySize.width) {
            x = ((parent.getWidth() - mySize.width) / 2) + topLeft.x;
        } else {
            x = topLeft.x;
        }

        if (parentSize.height > mySize.height) {
            y = ((parent.getHeight() - mySize.height) / 2) + topLeft.y;
        } else {
            y = topLeft.y;
        }
        setLocation(x, y);       
        loadListRoomType();
        if(listSelectedRoom.size() > 0){
            loadSelectedRoom();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbTip = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstRoom = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstSelected = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfTip = new javax.swing.JTextField();
        tfCost = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("THÔNG TIN PHÒNG");

        cbTip.setModel(this.dcbmRoomType);
        cbTip.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTipItemStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel1.setText("Loại phòng:");

        lstRoom.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        lstRoom.setModel(this.dlmRoom);
        lstRoom.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstRoomValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstRoom);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("DANH SÁCH PHÒNG: ");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("DANH SÁCH PHÒNG ĐƯỢC CHỌN:");

        lstSelected.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        lstSelected.setModel(this.dlmSelectedRoom);
        lstSelected.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstSelectedValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(lstSelected);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel4.setText("Loại:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel5.setText("Giá:");

        tfTip.setEditable(false);
        tfTip.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        tfTip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfTipActionPerformed(evt);
            }
        });

        tfCost.setEditable(false);
        tfCost.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel6.setText("(VNĐ)");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jButton1.setText(">>");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jButton2.setText("<<");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Chọn");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Hủy");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbTip, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(jLabel2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(34, 34, 34)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(78, 78, 78))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(jLabel3)
                        .addGap(0, 39, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(51, 51, 51)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfTip, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                    .addComponent(tfCost, javax.swing.GroupLayout.Alignment.LEADING))
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbTip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(jButton1)
                                .addGap(18, 18, 18)
                                .addComponent(jButton2))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfTip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfCost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        listSelectedRoom = null;      
        this.dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
            if(!listSelectedRoom.isEmpty()&&!lstSelected.isSelectionEmpty()){
                listSelectedRoom.remove(lstSelected.getSelectedIndex());
                listSelectedRoom.trimToSize();
                loadListRoom();
                loadSelectedRoom();
            }else{
                 JOptionPane.showMessageDialog(this, "Chưa chọn phòng"); 
            }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void lstRoomValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstRoomValueChanged
        if(lstRoom.getSelectedIndex() > -1){
            tfTip.setText(listRoom.get(lstRoom.getSelectedIndex()).getRoomtype().getName());
            tfCost.setText(new Integer(listRoomType.get(cbTip.getSelectedIndex()).getCost()).toString());
        }
    }//GEN-LAST:event_lstRoomValueChanged

    private void cbTipItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbTipItemStateChanged
                 loadListRoom();
    }//GEN-LAST:event_cbTipItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       if(cbTip.getSelectedIndex() > -1&&!lstRoom.isSelectionEmpty()){
           System.out.println(cbTip.getSelectedIndex());
           if((!listRoom.isEmpty())&&(dlmRoom.getElementAt(lstRoom.getSelectedIndex()) != "")){
               Room room = listRoom.get(lstRoom.getSelectedIndex());
               listSelectedRoom.add(room);
               listRoom.remove(room);
               listRoom.trimToSize();
               dlmSelectedRoom.addElement(room.getName());
               dlmRoom.removeElementAt(lstRoom.getSelectedIndex());
               
           }
       }else{
        JOptionPane.showMessageDialog(this, "Chưa chọn phòng"); 
       }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void lstSelectedValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstSelectedValueChanged
        if(lstSelected.getSelectedIndex() > -1){
            Room room = listSelectedRoom
                    .get(lstSelected.getSelectedIndex());
            tfTip.setText(room.getRoomtype().getName());
            tfCost.setText(new Integer(roomTypeService.getTypeRoomById(room.getType()).getCost()).toString());
        }
    }//GEN-LAST:event_lstSelectedValueChanged

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        parent.setListRoom(this.listSelectedRoom);
        listSelectedRoom = null;
        this.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void tfTipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfTipActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfTipActionPerformed


    public void loadListRoomType(){
        listRoomType = roomTypeService.getAllType();
        dcbmRoomType.removeAllElements();
        for(RoomType typeRoom : listRoomType){
            dcbmRoomType.addElement(typeRoom.getName());
        }
    }
    
    public void loadListRoom(){
        if(cbTip.getSelectedIndex() > -1){
            listRoom = roomService.getAllRoomByTypeAndStatus(listRoomType.get(cbTip.getSelectedIndex()).getId(),Constants.EMPTY);
            if((!listSelectedRoom.isEmpty()) && (!listRoom.isEmpty())){
                List<Room> listTemp = new ArrayList<>();
                for(Room r1 : listSelectedRoom){                   
                    for(Room r2 : listRoom){
                        if(r2.getId() == r1.getId()){
                            listTemp.add(r2);
                        }
                    }
                 listRoom.removeAll(listTemp);
                 listRoom.trimToSize();
                }
            }
            dlmRoom.removeAllElements();
            for(Room room : listRoom){
                dlmRoom.addElement(room.getName());
            }
        }
        if(listRoom.size()==0){
            tfTip.setText(null);
            tfCost.setText(null);
        }


    }
    public void loadSelectedRoom(){
        dlmSelectedRoom.removeAllElements();
        for(Room room : listSelectedRoom){
            dlmSelectedRoom.addElement(room.getName());
        }
        if(listSelectedRoom.isEmpty()){
            tfTip.setText(null);
            tfCost.setText(null);
        }
    }

    private ArrayList<Room> listSelectedRoom;
    private ArrayList<RoomType> listRoomType;
    private ArrayList<Room> listRoom;
    private DefaultComboBoxModel dcbmRoomType;
    private DefaultListModel dlmRoom;
    private DefaultListModel dlmSelectedRoom;
    private dao.Room roomService;
    private dao.RoomType roomTypeService;
    private GeneralInf parent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbTip;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstRoom;
    private javax.swing.JList lstSelected;
    private javax.swing.JTextField tfCost;
    private javax.swing.JTextField tfTip;
    // End of variables declaration//GEN-END:variables
}
